﻿using System;
using System.Globalization;

namespace IupNET
{
    internal static class Convert
    {
        internal static IntPtr[] ControlArrayToPtrArray(Control[] carr)
        {

            IntPtr[] ptrs = new IntPtr[carr.Length];
            int pos=0;
            foreach (Control ctrl in carr)
            {
                ptrs[pos++] = ctrl.cobject;
            }
            return ptrs;
        }

        internal static Color StringToColor(string colstr)
        {
            if (colstr == null || colstr == "")
                return Color.Empty;
            string[] strs=colstr.Split(new char[]{' '},StringSplitOptions.RemoveEmptyEntries);
            if (strs.Length != 3)
                throw new Exception("Invalid color string");
            byte r = byte.Parse(strs[0]);
            byte g = byte.Parse(strs[1]);
            byte b = byte.Parse(strs[2]);
            return Color.FromRGB(r, g, b);
        }

        internal static string ColorToString(Color c)
        {
            return c.R.ToString() + " " + c.G.ToString() + " " + c.B.ToString();
        }

        internal static Size2i StringToSize(string sizestr)
        {
            if (sizestr == null || sizestr == "")
                return new Size2i(0, 0);
            string[] strs = sizestr.Split(new char[] { ' ','x'}, StringSplitOptions.RemoveEmptyEntries);
            if (strs.Length != 2)
                throw new Exception("Invalid size string");
            int w = int.Parse(strs[0]);
            int h = int.Parse(strs[1]);
            return new Size2i(w, h);
        }

        internal static string SizeToString(Size2i s)
        {
            return s.Width.ToString() + "x" + s.Height.ToString();
        }

        internal static Point2i StringToPoint(string pointstr)
        {
            if (pointstr == null || pointstr == "")
                return new Point2i(0, 0);
            string[] strs = pointstr.Split(new char[] { ' ', ',',':','x' }, StringSplitOptions.RemoveEmptyEntries);
            if (strs.Length != 2)
                throw new Exception("Invalid size string");
            int w = int.Parse(strs[0]);
            int h = int.Parse(strs[1]);
            return new Point2i(w, h);
        }

        internal static string PointToString(Point2i s, string sepchar)
        {
            return s.X.ToString() + sepchar + s.Y.ToString();
        }

        internal static int StringToInt(string p)
        {
            if (p == null || p == "")
                return 0;
            return int.Parse(p);
        }

        internal static string AlignToString(Alignment a)
        {
            switch (a)
            {
                case Alignment.Left: return "ALEFT:ACENTER";
                case Alignment.Right: return "ARIGHT:ACENTER";
                case Alignment.Top: return "ACENTER:ATOP";
                case Alignment.Bottom: return "ACENTER:ABOTTOM";

                case Alignment.BottomLeft: return "ALEFT:ABOTTOM";
                case Alignment.BottomRight: return "ARIGHT:ABOTTOM";
                case Alignment.TopLeft: return "ALEFT:ATOP";
                case Alignment.TopRight: return "ARIGHT:ATOP";

                default: return "ACENTER:ACENTER";
            }
        }

        internal static Alignment StringToAlign(string a)
        {
            if (a == null || a == "")
                return Alignment.Center;

            switch (a)
            {
                case "ALEFT:ACENTER": return Alignment.Left;
                case "ARIGHT:ACENTER": return Alignment.Right;
                case "ACENTER:ABOTTOM": return Alignment.Bottom;
                case "ACENTER:ATOP": return Alignment.Top;
                case "ALEFT:ABOTTOM": return Alignment.BottomLeft;
                case "ARIGHT:ABOTTOM":  return Alignment.BottomRight;
                case "ALEFT:ATOP":    return Alignment.TopLeft;
                case "ARIGHT:ATOP":   return Alignment.TopRight;
                default: return Alignment.Center;
            }

        }


        internal static string ZBoxAlignToString(Alignment a)
        {
            switch (a)
            {
                case Alignment.Left: return "WEST";
                case Alignment.Right: return "EAST";
                case Alignment.Top: return "NORTH";
                case Alignment.Bottom: return "SOUTH";

                case Alignment.BottomLeft: return "SW";
                case Alignment.BottomRight: return "SE";
                case Alignment.TopLeft: return "NW";
                case Alignment.TopRight: return "NE";

                default: return "ACENTER";
            }
        }

        internal static Alignment StringToAlignZBox(string a)
        {
            if (a == null || a == "")
                return Alignment.Center;

            switch (a)
            {
                case "WEST": return Alignment.Left;
                case "EAST": return Alignment.Right;
                case "SOUTH": return Alignment.Bottom;
                case "NORTH": return Alignment.Top;
                case "SW": return Alignment.BottomLeft;
                case "SE": return Alignment.BottomRight;
                case "NW": return Alignment.TopLeft;
                case "NE": return Alignment.TopRight;
                default: return Alignment.Center;
            }

        }

        internal static string PositionToString(TabPosition a)
        {
            switch (a)
            {
                case TabPosition.Right: return "RIGHT";
                case TabPosition.Top: return "TOP";
                case TabPosition.Bottom: return "BOTTOM";
                default: return "LEFT";
            }
        }

        internal static TabPosition StringToPosition(string a)
        {
            if (a == null || a == "")
                return TabPosition.Left;

            switch (a)
            {
                case "RIGHT": return TabPosition.Right;
                case "BOTTOM": return TabPosition.Bottom;
                case "TOP": return TabPosition.Top;
                default: return TabPosition.Left;
            }

        }


        internal static string NormSizeToString(NormalizeSize a)
        {
            switch (a)
            {
                case NormalizeSize.Vertical: return "VERTICAL";
                case NormalizeSize.Horizontal: return "HORIZONTAL";
                case NormalizeSize.Both: return "BOTH";
                default: return "NO";
            }
        }

        internal static NormalizeSize StringToNormSize(string a)
        {
            if (a == null || a == "")
                return NormalizeSize.No;

            switch (a)
            {
                case "VERTICAL": return NormalizeSize.Vertical;
                case "HORIZONTAL": return NormalizeSize.Horizontal;
                case "BOTH": return NormalizeSize.Both;
                default: return NormalizeSize.No;
            }
        }

       /* internal static string SeparatorToString(Separator a) {
            return NormSizeToString((NormalizeSize)a);
        }

        internal static Separator StringToSeparator(string s) {
            return (Separator)StringToNormSize(s);

        }*/

        internal static Cursor StringToCursor(string a)
        {

            switch (a)
            {
                case "NONE":
                case "NULL": return Cursor.None;
                case "ARROW": return Cursor.Arrow;
                case "BUSY": return Cursor.Busy;
                case "CROSS": return Cursor.Cross;
                case "HAND": return Cursor.Hand;
                case "HELP": return Cursor.Help;
                case "MOVE": return Cursor.Move;
                case "PEN": return Cursor.Pen;
                case "RESIZE_N": return Cursor.ResizeN;
                case "RESIZE_S": return Cursor.ResizeS;
                case "RESIZE_NS": return Cursor.ResizeNS;
                case "RESIZE_W": return Cursor.ResizeW;
                case "RESIZE_E": return Cursor.ResizeE;
                case "RESIZE_WE": return Cursor.RezizeWE;
                case "RESIZE_NE": return Cursor.ResizeNE;
                case "RESIZE_SW": return Cursor.ResizeSW;
                case "RESIZE_NW": return Cursor.ResizeNW;
                case "RESIZE_SE": return Cursor.ResizeSE;
                case "TEXT": return Cursor.Text;
                case "APPSTARTING": return Cursor.AppStarting;
                case "NO": return Cursor.No;
                case "UPPARROW": return Cursor.UpArrow;
                default: return Cursor.Arrow;
            }
        }

        internal static string CursorToString(Cursor c)
        {
            switch(c) {
            case Cursor.None: return "NONE";

                case Cursor.Arrow:  return "ARROW";
                case Cursor.Busy: return "BUSY";
                case Cursor.Cross: return "CROSS";
                case Cursor.Hand: return "HAND";
                case Cursor.Help: return "HELP";
                case Cursor.Move: return "MOVE";
                case Cursor.Pen: return "PEN";
                case Cursor.ResizeN: return "RESIZE_N";
                case Cursor.ResizeS: return "RESIZE_S";
                case Cursor.ResizeNS: return "RESIZE_NS";
                case Cursor.ResizeW: return "RESIZE_W";
                case Cursor.ResizeE: return "RESIZE_E";
                case Cursor.RezizeWE: return "RESIZE_WE";
                case Cursor.ResizeNE: return "RESIZE_NE";
                case Cursor.ResizeSW: return "RESIZE_SW";
                case Cursor.ResizeNW: return "RESIZE_NW";
                case Cursor.ResizeSE: return "RESIZE_SE";
                case Cursor.Text: return "TEXT";
                case Cursor.AppStarting: return "APPSTARTING";
                case Cursor.No: return "NO";
                case Cursor.UpArrow: return "UPPARROW";
                default: return "ARROW";
            }
        }

        internal static double StringToReal(string r)
        {
            double res;
            if (double.TryParse(r, NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                return res;
            return 0.0;
        }

        internal static string RealToString(double r)
        {
            return r.ToString(CultureInfo.InvariantCulture);
        }


        internal static string ExpandToString(Expand a)
        {
            switch (a)
            {
                case Expand.Vertical: return "VERTICAL";
                case Expand.Horizontal: return "HORIZONTAL";
                case Expand.Yes: return "YES";
                default: return "NO";
            }
        }

        internal static Expand StringToExpand(string a)
        {
            if (a == null || a == "")
                return Expand.No;

            switch (a)
            {
                case "VERTICAL": return Expand.Vertical;
                case "HORIZONTAL": return Expand.Horizontal;
                case "YES": return Expand.Yes;
                default: return Expand.No;
            }
        }

        internal static string ScrollbarToString(Scrollbar a)
        {
            switch (a)
            {
                case Scrollbar.Vertical: return "VERTICAL";
                case Scrollbar.Horizontal: return "HORIZONTAL";
                case Scrollbar.Yes: return "YES";
                default: return "NO";
            }
        }

        internal static Scrollbar StringToScrollbar(string a)
        {
            if (a == null || a == "")
                return Scrollbar.No;

            switch (a)
            {
                case "VERTICAL": return Scrollbar.Vertical;
                case "HORIZONTAL": return Scrollbar.Horizontal;
                case "Yes": return Scrollbar.Yes;
                default: return Scrollbar.No;
            }
        }


        internal static string TickPosToString(TickPos a)
        {
            switch (a)
            {
                case TickPos.Both: return "BOTH";
                case TickPos.Reverse: return "REVERSE";
                default: return "NORMAL";
            }
        }

        internal static TickPos StringToTickPos(string a)
        {
            if (a == null || a == "")
                return TickPos.Normal;

            switch (a)
            {
                case "BOTH": return TickPos.Both;
                case "REVERSE": return TickPos.Reverse;
                default: return TickPos.Normal;
            }
        }

        internal static string PlacementToString(Placement a)
        {
            switch (a)
            {
                case Placement.Full: return "FULL";
                case Placement.Maximized: return "MAXIMIZED";
                case Placement.Minimized: return "MINIMIZED";
                default: return "NORMAL";
            }
        }

        internal static Placement StringToPlacement(string a)
        {
            if (a == null || a == "")
                return Placement.Normal;

            switch (a)
            {
                case "FULL": return Placement.Full;
                case "MAXIMIZED": return Placement.Maximized;
                case "MINIMIZED": return Placement.Minimized;
                default: return Placement.Normal;
            }

        }


        internal static string FileDialogTypeToString(FileDialogType a)
        {
            switch (a)
            {
                case FileDialogType.Open: return "OPEN";
                case FileDialogType.Save: return "SAVE";
                case FileDialogType.Dir: return "DIR";
                default: return "OPEN";
            }
        }

        internal static FileDialogType StringToFileDialogType(string a)
        {
            if (a == null || a == "")
                return FileDialogType.Open;

            switch (a)
            {
                case "OPEN": return FileDialogType.Open;
                case "SAVE": return FileDialogType.Save;
                case "DIR": return FileDialogType.Dir;
                default: return FileDialogType.Open;
            }

        }


        internal static MessageDialogButtons StringToMessageDialogButtons(string a)
        {

            if (a == null || a == "")
                return MessageDialogButtons.Ok;

            switch (a)
            {
                case "OK": return MessageDialogButtons.Ok;
                case "OKCANCEL": return MessageDialogButtons.OkCancel;
                case "YESNO": return MessageDialogButtons.YesNo;
                default: return MessageDialogButtons.Ok;
            }
        }

        internal static string MessageDialogButtonsToString(MessageDialogButtons a)
        {
            switch (a)
            {
                case MessageDialogButtons.Ok: return "OK";
                case MessageDialogButtons.OkCancel: return "OKCANCEL";
                case MessageDialogButtons.YesNo: return "YESNO";
                default: return "OK";
            }
        }

        internal static MessageDialogType StringToMessageDialogType(string a)
        {
            if (a == null || a == "")
                return MessageDialogType.Message;
            
            switch (a)
            {
                case "MESSAGE": return MessageDialogType.Message;
                case "ERROR": return MessageDialogType.Error;
                case "WARNING": return MessageDialogType.Warning;
                case "QUESTION": return MessageDialogType.Question;
                case "INFORMATION": return MessageDialogType.Question;
                default: return MessageDialogType.Message;
            }
        }

        internal static string MessageDialogTypeToString(MessageDialogType a)
        {
            switch (a)
            {
                case MessageDialogType.Message: return "MESSAGE";
                case MessageDialogType.Question: return "QUESTION";
                case MessageDialogType.Warning: return "WARNING";
                case MessageDialogType.Information: return "INFORMATION";
                case MessageDialogType.Error: return "ERROR";
                default: return "MESSAGE";
            }
        }

        internal static Direction StringToDirection(string a)
        {
            if (a == null || a == "")
                return Direction.East;

            switch (a)
            {
                case "NORTH": return Direction.North;
                case "WEST": return Direction.West;
                case "SOUTH": return Direction.South;
                default: return Direction.East;
            }
        }

        internal static string DirectionToString(Direction a)
        {
            switch (a)
            {
                case Direction.North: return "NORTH";
                case Direction.West: return "WEST";
                case Direction.South: return "SOUTH";
                default: return "EAST";
            }
        }
    }
}
